#!/bin/sh

_USER_="www-data"
_GROUP_="www-data"

# 目录权限
cd /data && mkdir -p cache && \
chown -R ${_USER_}:${_GROUP_} bin cache logs

# 配置文件
if [[ "consul" == "${KV_TYPE}" ]]; then
    #
    # 从 Consul 和 KV 存储中下载配置文件.
    #
    # ENV KV_TYPE=consul
    # ENV CONSUL_HOST=192.168.0.100:8500
    # ENV CONSUL_NAME=canal/app
    # ENV CONSUL_PATH=/data/conf
    su-exec ${_USER_}:${_GROUP_} /usr/local/bin/kv "${KV_TYPE}" \
        --host="${CONSUL_HOST:-127.0.0.1:8500}" \
        --path="${CONSUL_PATH:-/data/conf}" \
        --name="${CONSUL_NAME:-canal}" \
        --override
fi

# 启动服务
cd /data/bin && \
exec su-exec ${_USER_}:${_GROUP_} /bin/bash startup.sh
